function createCompanyInfoModel(data) {
	var companyInformation = data.getElementsByTagName("CompanyInformation");
	var name = companyInformation[0].getElementsByTagName("CompanyName")[0].childNodes[0].nodeValue;
	var address = companyInformation[0].getElementsByTagName("Address")[0].childNodes[0].nodeValue;
	var phone = companyInformation[0].getElementsByTagName("Phone")[0].childNodes[0].nodeValue;
	var email = companyInformation[0].getElementsByTagName("Email")[0].childNodes[0].nodeValue;
	var desc = companyInformation[0].getElementsByTagName("Description")[0].childNodes[0].nodeValue;
	var info = {
		"companyName" : name,
		"companyAddress" : address,
		"companyPhone" : phone,
		"companyEmail" : email,
		"companyDesc" : desc
	}
	return new Information(info);
}

var Information = function(information) {
	this.information = information;
}
function showCompanyInfoPage(data) {
	var key = "companyInfo";
	addActive(key);

	$id("txtComName").value = data.companyName;
	$id("txtComPhone").value = data.companyPhone;
	$id("txtComAddress").value = data.companyAddress;
	$id("txtComEmail").value = data.companyEmail;
	$id("txtComDes").value = data.companyDesc;
	
}

function updateComInfo() {
	var name = document.getElementById("txtComName").value;
	var phone = document.getElementById("txtComPhone").value;
	var address = document.getElementById("txtComAddress").value;
	var email = document.getElementById("txtComEmail").value;
	var des = document.getElementById("txtComDes").value;
	if (validateCompanyInfo(name, phone, address, email, des)) {

		ajax.put({
			url : "http://localhost:8080/book-service/admin/info",
			param : {
				"CompanyName" : name,
				"Address" : address,
				"Phone" : phone,
				"Email" : email,
				"Description" : des
			},
			success : function() {
				alert("Cập nhật thông tin cửa hàng thành công!");
				getCompanyInfo();
			},
			failure : function(xhr) {
				if (xhr.status == 401) {
					alert("Bạn không có quyền truy cập vào trang này!\n");
				} else {
					alert("Có lỗi xảy ra. Xin thử lại!");
				}
			},
			abort : function(xhr) {
				callback.abort(xhr);
			}
		});
	}

}

function getCompanyInfo() {
	ajax.get({
		url : "http://localhost:8080/book-service/admin/info",
		success : function(data, xhr) {
			showPage("content-information");
			var model = createCompanyInfoModel(data);
			if (model.information.length <= 0) {
				alert("Không có kết quả!");
			}
			showCompanyInfoPage(model.information);

		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!");
				
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

